using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Devices.Sensors;
using System.Windows;
using Microsoft.Xna.Framework;

namespace Balloon
{
    public class AclReadEventArgs : EventArgs
    {
        public AccelerometerReading AclReading;
    }

    public delegate void AclReadEventHandler(object sender, AclReadEventArgs e);

    public class Acl
    {
        public event AclReadEventHandler AclRead;

        Accelerometer _acl;
        const int _timeout = 1000;

        public Acl()
        {
        }

        public void Initialize()
        {
            _acl = new Accelerometer();
            _acl.TimeBetweenUpdates = TimeSpan.FromMilliseconds(_timeout);
            _acl.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(current_value_changed);
        }

        public void Start()
        {
            _acl.Start();
        }

        private void current_value_changed(object sender, SensorReadingEventArgs<AccelerometerReading> e)
        {
            // Call Reading on the UI thread and pass the AccelerometerReading.
            Deployment.Current.Dispatcher.BeginInvoke(() => update_acl(e.SensorReading));
        }

        private void update_acl(AccelerometerReading e)
        {
            AclReadEventArgs arg = new AclReadEventArgs();
            arg.AclReading = e;
            AclRead(this, arg);
        }
    }
}
